/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.module.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.framework.common.exception.ServiceException;
import com.easy.framework.common.utils.Func;
import com.easy.module.system.cache.DeptCache;
import com.easy.module.system.entity.SysDept;
import com.easy.module.system.entity.SysPost;
import com.easy.module.system.entity.SysUserDept;
import com.easy.module.system.mapper.SysPostMapper;
import com.easy.module.system.query.SysPostQuery;
import com.easy.module.system.service.ISysPostService;
import com.easy.module.system.service.ISysUserDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 岗位信息表 Service业务
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-19
 */
@RequiredArgsConstructor
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    private final ISysUserDeptService userDeptService;

    /**
     * 查询岗位信息列表
     *
     * @param query 岗位信息
     * @return 岗位信息
     */
    @Override
    public List<SysPost> list(SysPostQuery query) {
        List<SysPost> list = list(lambdaQuery(query));
        if (Func.isNotEmpty(list)) {
            list.forEach(item -> {
                item.setDeptName(DeptCache.get(item.getDeptId()).map(SysDept::getDeptName).orElse(""));
            });
            list.sort((o1, o2) -> o1.getSort().compareTo(o2.getSort()));
        }
        return list;
    }

    /**
     * 新增岗位信息
     *
     * @param dto 岗位信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(SysPost dto) {
        return save(dto);
    }

    /**
     * 修改岗位信息
     *
     * @param dto 岗位信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysPost dto) {
        return updateById(dto);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Long[] postIds) {
        for (Long postId : postIds) {
            SysPost post = getById(postId);
            if (userDeptService.count(Wrappers.<SysUserDept>lambdaQuery().eq(SysUserDept::getId, postId)) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return removeBatchByIds(List.of(postIds));
    }

    @Override
    public List<SysPost> getListByUserId(Long userId) {
        List<Long> postIdList = userDeptService.getPostIdListByUserId(userId);
        if (Func.isNotEmpty(postIdList)) {
            return listByIds(postIdList);
        }
        return null;
    }
}
